/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.controllers;

import br.com.database.DaoClassRoom;
import br.com.database.DaoDays;
import br.com.database.DaoDiscipline;
import br.com.database.DaoDisciplineRequisite;
import br.com.database.DaoGraduation;
import br.com.database.DaoTeacher;
import br.com.database.DaoTeacherDays;
import br.com.database.DaoTeacherGraduation;
import br.com.models.ClassRoom;
import br.com.models.Days;
import br.com.models.Discipline;
import br.com.models.Teacher;
import java.util.ArrayList;

/**
 *
 * @author Valdigleis
 */
public class Controller {
    
    private DaoTeacher daoTeacher;
    
    private DaoGraduation daoGraduation;
    
    private DaoDays daodays;
    
    private DaoTeacherDays daoTeacherDays;
    
    private DaoTeacherGraduation daoTeacherGraduation;
    
    private DaoDiscipline daoDiscipline;
    
    private DaoDisciplineRequisite daoDisciplineRequisite;
    
    private DaoClassRoom daoClassRoom;;
    
    /**
     * Método responsavel por implementar um ponte
     * entre a GUI e os Objetos DAO's.
     * 
     * @param teacher 
     */
    public void saveTeacher(Teacher teacher){
       
        this.daoTeacher = new DaoTeacher();
        this.daoTeacher.save(teacher);
        
        this.daoGraduation = new DaoGraduation();
        daoGraduation.save(teacher.getGraduation());
        
        this.daoTeacherDays = new DaoTeacherDays();
        this.daoTeacherDays.salveExpedient(this.daoTeacher.idFinalTeacher(), teacher.getDays());
        
        
        this.daoTeacherGraduation = new DaoTeacherGraduation();
        this.daoTeacherGraduation.saveTeacher_Graduation(this.daoTeacher.idFinalTeacher(), this.daoGraduation.idFinalGraduation());
        
        this.daoTeacher.closeConnection();
        this.daoGraduation.closeConnection();
        
    }
        
    /**
     * Método que lista todos os professores na base de dados.
     * @return 
     */
    public ArrayList<Teacher> seacherAllTeacher(){
        
        this.daoTeacher = new DaoTeacher();
        ArrayList<Teacher> teachers = daoTeacher.seacherAllTeacher();
        daoTeacher.closeConnection();
        
        int aux;
        this.daoGraduation = new DaoGraduation();
        this.daoTeacherGraduation = new DaoTeacherGraduation();
        int i;
        for(i = 0; i < teachers.size(); i++){
            aux = daoTeacherGraduation.seacherGraduationOnTeacher(teachers.get(i).getId());
            teachers.get(i).setGraduation(daoGraduation.getGaduation(aux));
        }
        daoTeacherGraduation.closeConnection();
        daoGraduation.closeConnection();
        
        this.daoTeacherDays = new DaoTeacherDays();
        this.daodays = new DaoDays();
        ArrayList<Integer> auxDays = new ArrayList<Integer>();
        for(i = 0; i < teachers.size(); i++){
            auxDays = daoTeacherDays.getExpedient(teachers.get(i).getId());
            teachers.get(i).setDays(daodays.seacherDays(auxDays));
        }
        this.daoTeacherDays.closeConnection();
        this.daodays.closeConnection();
        
        return teachers;
    }
        
    /**
     * Método que atualiza um professor na base de dados.
     * @param teacher 
     */
    public void updateTeacher(Teacher teacher){
        this.daoTeacher = new DaoTeacher();
        daoTeacher.update(teacher);
        daoTeacher.closeConnection();
        
        this.daoGraduation = new DaoGraduation();
        daoGraduation.update(teacher.getGraduation());
        daoGraduation.closeConnection();
        
        this.daoTeacherDays = new DaoTeacherDays();
        daoTeacherDays.removerExpedient(teacher.getId());
        this.daoTeacherDays.salveExpedient(teacher.getId(), teacher.getDays());
        this.daoTeacherDays.closeConnection();
    }
    
    //public void removerTeacher(Teacher teacher)
    
    /**
     * Método que checa a existência de uma matricula na base dos
     * professores.
     * @param registerTeacher
     * @return 
     */
    public boolean checkRegister(String registerTeacher){
        this.daoTeacher = new DaoTeacher();
        return daoTeacher.checkRegister(registerTeacher);
    }
    
    //--------------
    //Trata das Disciplinas
    
    public void saveDiscipline(Discipline discipline){
        this.daoDiscipline = new DaoDiscipline();
        this.daoDiscipline.Save(discipline);
        this.daoDiscipline.closeConnection();
    }
    
    public void saveDisciplineAndRequisitos(Discipline discipline){
        this.daoDisciplineRequisite = new DaoDisciplineRequisite();
        this.daoDiscipline = new DaoDiscipline();
        this.daoDiscipline.Save(discipline);
        int aux = this.daoDiscipline.getIdFinal();
        this.daoDiscipline.closeConnection();
        for(int i = 0; i < discipline.getPreRequisite().size(); i++){
            this.daoDisciplineRequisite.saveRequisites(aux, discipline.getPreRequisite().get(i).getId());
        }
    }
    
    public Discipline getDiscipline(int idDiscipline){
        this.daoDiscipline = new DaoDiscipline();
        Discipline discipline = this.daoDiscipline.getDisicipline(idDiscipline);
        this.daoDiscipline.closeConnection();
        return discipline;
    }
    
    public boolean checkCode(String code){
        this.daoDiscipline = new DaoDiscipline();
        return daoDiscipline.checkCode(code);
    }
    
    public void updateDiscipline(Discipline discipline){
        this.daoDiscipline = new DaoDiscipline();
        this.daoDiscipline.update(discipline);
        this.daoDiscipline.closeConnection();
        this.daoDisciplineRequisite = new DaoDisciplineRequisite();
        this.daoDisciplineRequisite.removerPre(discipline.getId());
        for(int i = 0; i < discipline.getPreRequisite().size(); i++){
            this.daoDisciplineRequisite.saveRequisites(discipline.getId(), discipline.getPreRequisite().get(i).getId());
        }
        this.daoDisciplineRequisite.closeConnection();
    }
    
    public ArrayList<Discipline> seacherAllDiscipline(){
        this.daoDiscipline = new DaoDiscipline();
        ArrayList<Discipline> disciplines = this.daoDiscipline.getAllDiscipline();
        this.daoDisciplineRequisite = new DaoDisciplineRequisite();
        for(int i = 0; i < disciplines.size(); i++){
            ArrayList<Integer> aux = this.daoDisciplineRequisite.getAllPre(disciplines.get(i).getId());
            ArrayList<Discipline> pre = new ArrayList<Discipline>();
            
                for(int j = 0; j < aux.size(); j++){
                    pre.add(this.daoDiscipline.getDisicipline(aux.get(j)));
                }
                disciplines.get(i).setPreRequisite(pre);
            
        }
        this.daoDiscipline.closeConnection();
        this.daoDisciplineRequisite.closeConnection();
        return disciplines;
    }
    
    public ArrayList<Discipline> seacherAllDisciplineSemester(int semester){
        this.daoDiscipline = new DaoDiscipline();
        ArrayList<Discipline> disciplines = this.daoDiscipline.getAllDiscipline(semester);
        this.daoDisciplineRequisite = new DaoDisciplineRequisite();
        for(int i = 0; i < disciplines.size(); i++){
            ArrayList<Integer> aux = this.daoDisciplineRequisite.getAllPre(disciplines.get(i).getId());
            ArrayList<Discipline> pre = new ArrayList<Discipline>();
            
                for(int j = 0; j < aux.size(); j++){
                    pre.add(this.daoDiscipline.getDisicipline(aux.get(j)));
                }
                disciplines.get(i).setPreRequisite(pre);
            
        }
        this.daoDiscipline.closeConnection();
        this.daoDisciplineRequisite.closeConnection();
        return disciplines;
    }
    
    public boolean existenceOfClass(Discipline discipline, String semester){
        this.daoClassRoom = new DaoClassRoom();
        return this.daoClassRoom.checkExisteceClass(discipline, semester);
    }
    
    public ArrayList<Discipline> seacherAllDiferentsOFDiscipline(int id){
        this.daoDiscipline = new DaoDiscipline();
        ArrayList<Discipline> disciplines = this.daoDiscipline.getAllDiferentsOfDiscipline(id);
        this.daoDisciplineRequisite = new DaoDisciplineRequisite();
        for(int i = 0; i < disciplines.size(); i++){
            ArrayList<Integer> aux = this.daoDisciplineRequisite.getAllPre(disciplines.get(i).getId());
            ArrayList<Discipline> pre = new ArrayList<Discipline>();
            
                for(int j = 0; j < aux.size(); j++){
                    pre.add(this.daoDiscipline.getDisicipline(aux.get(j)));
                }
                disciplines.get(i).setPreRequisite(pre);
            
        }
        this.daoDiscipline.closeConnection();
        this.daoDisciplineRequisite.closeConnection();
        return disciplines;
    }
    
    // Pega o numero de disciplinas que abre!
    public ArrayList<Integer> getIdAllPosRequisitos(int id ){
        this.daoDisciplineRequisite = new DaoDisciplineRequisite();
        ArrayList<Integer> pos = this.daoDisciplineRequisite.getPos(id);
        this.daoDisciplineRequisite.closeConnection();
        return pos;
    }
    
    //-------------------------------
    // Trata das turmas.
    
    public void saveClassRoom(ClassRoom classRoom){
        this.daoClassRoom = new DaoClassRoom();
        this.daoClassRoom.saveClassRoom(classRoom);
        this.daoClassRoom.closeConnection();
    }
    
    public int getNumberClass(Discipline discipline){
        this.daoClassRoom = new DaoClassRoom();
        ArrayList<ClassRoom> classRooms = this.daoClassRoom.seacherNumberClass(discipline.getId());
        if(classRooms.isEmpty()){
            this.daoClassRoom.closeConnection();
            return 1;
        }
        else{
            int i = 1;
            while(this.daoClassRoom.checkNumberClass(discipline, i)){
                i++;
            }
            this.daoClassRoom.closeConnection();
            return i;
        }
    }
    
    public ArrayList<ClassRoom> seacherByTeacher(Teacher teacher){
        this.daoClassRoom = new DaoClassRoom();
        this.daoDiscipline = new DaoDiscipline();
        ArrayList<ClassRoom> classRooms = this.daoClassRoom.seacherByTheTeacher(teacher.getId());
        for(int i = 0; i < classRooms.size(); i++){
            classRooms.get(i).setTeacher(teacher);
            classRooms.get(i).setDiscipline(this.daoDiscipline.getDisicipline(classRooms.get(i).getDiscipline().getId()));
        }
        return classRooms;
    }
    
    public ArrayList<ClassRoom> seacherByDiscipline(Discipline discipline){
        this.daoClassRoom = new DaoClassRoom();
        this.daoTeacher = new DaoTeacher();
        ArrayList<ClassRoom> classRooms = this.daoClassRoom.seacherByTheDiscipline(discipline.getId());
        for(int i = 0; i < classRooms.size(); i++){
            classRooms.get(i).setTeacher(this.daoTeacher.seacherTeacher(classRooms.get(i).getTeacher().getId()));
            classRooms.get(i).setDiscipline(discipline);
        }
        return classRooms;
    }
    
    public ArrayList<ClassRoom> getClassDiscipline(Discipline discipline, String semester){
        this.daoClassRoom = new DaoClassRoom();
        this.daoTeacher = new DaoTeacher();
        ArrayList<ClassRoom> classRooms = this.daoClassRoom.seacherByTheDisciplineAndSemster(discipline.getId(), semester);
        for(int i = 0; i < classRooms.size(); i++){
            classRooms.get(i).setTeacher(this.daoTeacher.seacherTeacher(classRooms.get(i).getTeacher().getId()));
            this.daoTeacherGraduation = new DaoTeacherGraduation();
            this.daoGraduation = new DaoGraduation();
            classRooms.get(i).getTeacher().setGraduation(this.daoGraduation.getGaduation(this.daoTeacherGraduation.seacherGraduationByTeacher(classRooms.get(i).getTeacher().getId())));
            this.daoGraduation.closeConnection();
            this.daoTeacherGraduation.closeConnection();
            this.daoTeacherDays = new DaoTeacherDays();
            this.daodays = new DaoDays();
            classRooms.get(i).getTeacher().setDays(this.daodays.seacherDays(this.daoTeacherDays.getExpedient(classRooms.get(i).getTeacher().getId())));
            this.daoTeacherDays.closeConnection();
            this.daodays.closeConnection();
            classRooms.get(i).setDiscipline(discipline);
        }
        return classRooms;
    }
    
    public ArrayList<ClassRoom> seacherByTeacherDiscipline(Teacher teacher, Discipline discipline){
        this.daoClassRoom = new DaoClassRoom();
        ArrayList<ClassRoom> classRooms = this.daoClassRoom.seacherByTheTeacherAndDiscipline(teacher.getId(), discipline.getId());
        for(int i = 0; i < classRooms.size(); i++){
            classRooms.get(i).setTeacher(teacher);
            classRooms.get(i).setDiscipline(discipline);
        }
        this.daoClassRoom.closeConnection();
        return classRooms;
    }
    
    public ArrayList<ClassRoom> getAllClassRoom(String semester){
        this.daoClassRoom = new DaoClassRoom();
        ArrayList<ClassRoom> classRooms = this.daoClassRoom.seacherAll(semester);
        this.daoClassRoom.closeConnection();
        this.daoGraduation = new DaoGraduation();

        for(int i = 0; i < classRooms.size() ; i++){
            
            this.daoTeacher = new DaoTeacher();
            ClassRoom novo = new ClassRoom();
            novo.setTeacher(this.daoTeacher.seacherTeacherById(classRooms.get(i).getTeacher().getId()));
            this.daoTeacher.closeConnection();
            
            this.daoTeacherGraduation = new DaoTeacherGraduation();
            this.daoGraduation = new DaoGraduation();
            int x = this.daoTeacherGraduation.seacherGraduationOnTeacher(novo.getTeacher().getId());
            novo.getTeacher().setGraduation(this.daoGraduation.getGaduation(x));
            this.daoGraduation.closeConnection();
            this.daoTeacherGraduation.closeConnection();
            
            this.daoTeacherDays = new DaoTeacherDays();
            this.daodays = new DaoDays();
            novo.getTeacher().setDays(this.daodays.seacherDays(this.daoTeacherDays.getExpedient(novo.getTeacher().getId())));
            this.daoTeacherDays.closeConnection();
            this.daodays.closeConnection();
            
            this.daoDiscipline = new DaoDiscipline();
            novo.setDiscipline(this.daoDiscipline.getDisicipline(classRooms.get(i).getDiscipline().getId()));
            this.daoDisciplineRequisite = new DaoDisciplineRequisite();
            ArrayList<Integer> aux = this.daoDisciplineRequisite.getAllPre(novo.getDiscipline().getId());
            ArrayList<Discipline> pre = new ArrayList<Discipline>();
            for(int j = 0; j < aux.size(); j++){
                    pre.add(this.daoDiscipline.getDisicipline(aux.get(j)));
            }
            novo.getDiscipline().setPreRequisite(pre);
            this.daoDisciplineRequisite.closeConnection();
            this.daoDiscipline.closeConnection();
            
            classRooms.set(i, novo);
        }
        
        return classRooms;
    } 
    
    
    
    public void remove(ClassRoom classRoom){
        this.daoClassRoom = new DaoClassRoom();
        this.daoClassRoom.remove(classRoom);
        this.daoClassRoom.closeConnection();
    }
    
    public void remove(Teacher teacher){
        this.daoTeacher = new DaoTeacher();
        this.daoTeacher.remove(teacher);
        this.daoTeacher.closeConnection();
        this.daoTeacherDays = new DaoTeacherDays();
        this.daoTeacherDays.removerExpedient(teacher.getId());
        this.daoTeacherDays.closeConnection();
        this.daoClassRoom = new DaoClassRoom();
        this.daoClassRoom.removeByTeacher(teacher);
        this.daoClassRoom.closeConnection();
    }
    
    public void remove(Discipline discipline){
        this.daoDiscipline = new DaoDiscipline();
        this.daoDiscipline.remove(discipline);
        this.daoDiscipline.closeConnection();
        this.daoDisciplineRequisite = new DaoDisciplineRequisite();
        this.daoDisciplineRequisite.remove(discipline);
        this.daoDisciplineRequisite.closeConnection();
        this.daoClassRoom = new DaoClassRoom();
        this.daoClassRoom.removeByTeacher(discipline);
        this.daoClassRoom.closeConnection();
    }
    
    public Days getDay(int id){
        this.daodays = new DaoDays();
        return this.daodays.getDay(id);
    }
    
}